# users/adminx.py

import xadmin
from django.contrib.auth.models import User, Group

from xadmin.views.website import LoginView
from .models import EmailVerifyRecord,Banner,Department,UserProfile,UserWork
from xadmin import views
from django.db.models import Q

from django.contrib.auth import get_user_model

class LoginViewAdmin(LoginView):
    title = '勘察信息管理系统'

xadmin.site.register(LoginView, LoginViewAdmin)
# 创建xadmin的最基本管理器配置，并与view绑定
#增加注释
class BaseSetting(object):
    # 开启主题功能
    enable_themes = True
    use_bootswatch = True

# 全局修改，固定写法
class GlobalSettings(object):
    # 修改title
    site_title = '工程勘察管理系统'
    # 修改footer
    site_footer = '河南省豫北水利勘测设计院有限公司&勘测院'
    # 收起菜单
    menu_style = 'accordion'
    # # 设置models的全局图标
    # global_search_models = [UserProfile, Sports]
    # global_models_icon = {
    #     UserProfile: "glyphicon glyphicon-user", Sports: "fa fa-cloud"
    # }

#xadmin中这里是继承object，不再是继承admin
# class EmailVerifyRecordAdmin(object):
#     # 显示的列
#     list_display = ['code', 'email', 'send_type', 'send_time']
#     # 搜索的字段
#     search_fields = ['code', 'email', 'send_type']
#     # 过滤
#     list_filter = ['code', 'email', 'send_type', 'send_time']


# class BannerAdmin(object):
#     list_display = ['title', 'image', 'url','index', 'add_time']
#     search_fields = ['title', 'image', 'url','index']
#     list_filter = ['title', 'image', 'url','index', 'add_time']

# class UserProfileAdmin(object):
#     # list_display = ['username','nick_name','gender','mobile','idcard','department']
#     # form_layout = (
#     #             Main(
#     #                 Fieldset('',
#     #                          'username', 'password',
#     #                          css_class='unsort no_title'
#     #                          ),
#     #                 # Fieldset(_('Personal info'),
#     #                 #          Row('first_name', 'last_name'),
#     #                 #          'email'
#     #                 #          ),
#     #                 Fieldset(_('Permissions'),
#     #                          'groups', 'user_permissions'
#     #                          ),
#     #                 # Fieldset(_('Important dates'),
#     #                 #          'last_login', 'date_joined'
#     #                 #          ),
#     #             ),
#     #             Side(
#     #                 Fieldset(_('Status'),
#     #                          'is_active', 'is_staff', 'is_superuser',
#     #                          ),
#     #             ),
#     #         )
# xadmin.site.unregister(UserProfile)
# xadmin.site.register(UserProfile,UserProfileAdmin)

# class UserAdmin(object):
#     list_display = ['username', 'nick_name','department','is_active','is_staff','mobile','date_joined']
#     search_fields = ['username', 'email', 'is_staff']
#     list_filter = ['username', 'email', 'is_staff']

# xadmin.site.unregister(get_user_model())
# xadmin.site.register(get_user_model(),UserAdmin)

class DepartmentAdmin(object):
    list_display = ['department', 'isactive', 'addtime','mark']
    search_fields = ['department', 'isactive', 'addtime','mark']
    list_filter = ['department', 'isactive', 'addtime','mark']

class UserWorkAdmin(object):
    list_display = ['id','addproject_col','serviceclass_col','add_time','title','status','user','isopen']
    list_filter = ['status','user__nick_name','project__addproject__tempname','project__addproject__serviceclass']
    search_fields = ['title']
    exclude=['user'] #隐藏字段
    ordering = ['-add_time',]
    list_editable = ['user']

    def save_models(self):
        # print("------------------%s"%(self.user))
        # if not self.user:
        self.new_obj.user = self.request.user
        # # self.new_obj.save()
        super().save_models()

    def queryset(self):
        """
        函数作用：权限划分
        1.自己可以看到自己的所有记录
        2.部门负责可以看到isopen=ture的所有记录
        3.部门负责是本人，可以看到自己的全部记录和所有人的isopen=ture的记录
        4.超级管理员可以看到所有记录
        """
        qs = super(UserWorkAdmin, self).queryset()
        if self.request.user.is_superuser:#是否为超级管理员
            return qs
        elif self.request.user.groups.filter(name='勘测院-部分负责人').exists():#是否为部门负责人
            return qs.filter(Q(user=self.request.user)|Q(isopen=True))
        else:
            return qs.filter(user=self.request.user)

xadmin.site.register(UserWork,UserWorkAdmin)
# xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin)
# xadmin.site.register(Banner,BannerAdmin)
xadmin.site.register(Department,DepartmentAdmin)

# 将基本配置管理与view绑定
xadmin.site.register(views.BaseAdminView,BaseSetting)

# 将title和footer信息进行注册
xadmin.site.register(views.CommAdminView,GlobalSettings)